package com.chb.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.chb.properties.TestProperties;
import org.apache.ibatis.reflection.MetaObject;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @description:
 * @author: CHB
 * @create: 2022-04-12 14:40
 **/
public class FillMetaObjectHandler implements MetaObjectHandler {

    @Resource
    private TestProperties testProperties;

    @Override
    public void insertFill(MetaObject metaObject) {
        Date current = new Date();
        Object createTime = this.getFieldValByName("createTime", metaObject);
        if (createTime == null) {
            this.setFieldValByName("createTime", current, metaObject);
        }
        this.setFieldValByName("updateTime", current, metaObject);

        if (testProperties.getUserName() != null) {
            this.setFieldValByName("createUser", testProperties.getUserName(), metaObject);
            this.setFieldValByName("createUserName", testProperties.getUserName(), metaObject);
            this.setFieldValByName("updateUser", testProperties.getUserName(), metaObject);
            this.setFieldValByName("updateUserName", testProperties.getUserName(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if (testProperties.getUserName() != null) {
            this.setFieldValByName("updateUser", testProperties.getUserName(), metaObject);
            this.setFieldValByName("updateUserName", testProperties.getUserName(), metaObject);
        }
    }
}
